// pages/Users/UserInfo/UserInfo.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUser , updateUser } from '../../../api/user';
import { getGradeConfig , queryAppInforMation } from '../../../api/brand';
import { ACCESS_TOKEN } from '../../../utils/types';
import areaList from '../../../common/area';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    ip: app.globalData.global.http,
    user: {},
    gradeList: [],
    show: false,
    areaList: areaList,
    shopName: '',
    province: '',
    city: '',
    country: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getUser();
    this._getGradeConfig();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取用户个人信息
  getUser(){
    let _this = this;
    getUser()
    .then(res => {
      if(res.success){
        _this.setData({
          user: res.data,
          province: res.data.province,
          city: res.data.city,
          country: res.data.country,
          shopName: res.data.shopName
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取品牌等级配置信息
  _getGradeConfig(){
    let _this = this;
    getGradeConfig()
    .then(res => {
      if(res.success){
        _this.setData({
          gradeList: res.data.content
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 更换头像
  updateUserHeadPic(){
    let _this = this;
    var token = wx.getStorageSync(ACCESS_TOKEN)
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: (res) => {
        console.log(res,'res')
        // console.log(res.tempFiles.tempFilePath)
        wx.uploadFile({
          url: _this.data.ip + '/app/upload/user/headimg',
          filePath: res.tempFiles[0].tempFilePath,
          name: 'headimg',
          formData: {
            'access_token': token,
          },
          success: function(res){
            res = JSON.parse(res.data)
            if(res.success){
              wx.setStorageSync('hasUpdateUser', '1')
              _this.getUser();
            }else{
              wx.showToast({
                title: res.errMessage,
                icon: 'none',
              })
            }
          },
          fail: function(err){
            console.log(err)
          },
        })
      }
    })
  },
  // 选择省市区
  selCity(){
    this.setData({
      show: true,
    })
  },
  // 确定选择省市区
  onConfirm(e) {
    console.log(e.detail)
    this.setData({
      address: e.detail.values[0].name + '-' + e.detail.values[1].name,
      province: e.detail.values[0].name,
      city: e.detail.values[1].name,
      country: e.detail.values[2].name,
      show: false,
    })
  },
  submit(){
    let _this = this;
    updateUser({shopName: _this.data.shopName,province: _this.data.province,city: _this.data.city,country: _this.data.country,})
    .then(res => {
      if(res.success){
        _this.getUser();
        wx.setStorageSync('hasUpdateUser', '1')
        wx.showToast({
          title: '修改成功',
          icon: 'success',
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
      
    }
  },
  // 展示等级规则说明
  showGradeDesc(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    console.log(item,'item');
    if(!item.upgradeDescription){
      return;
    }
    queryAppInforMation({infoType: '11',title: '等级说明',page: 0})
      .then(res => {
        if(res.success){
          if(res.data.content.length > 0){
            var itm = res.data.content[0];
            if(res.data.content[0].imgUrl){
              wx.navigateTo({
                url: `/pages/Link/LinkImg/LinkImg?url=${encodeURIComponent(itm.imgUrl)}&title=${JSON.stringify(item.gradeName)}`,
              })
            }else if(res.data.content[0].jumpUrl){
              wx.navigateTo({
                url: `/pages/Link/LinkImg/LinkImg?url=${encodeURIComponent(itm.jumpUrl)}&title=${JSON.stringify(item.gradeName)}`,
              })
            }
          }
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
  },
})